package org.example.studentsystem_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.example.studentsystem_back.common.CommonResponse;
import org.example.studentsystem_back.domain.Freshmen;
import org.example.studentsystem_back.domain.Leaders;
import org.example.studentsystem_back.domain.Users;
import org.example.studentsystem_back.mapper.UsersMapper;
import org.example.studentsystem_back.service.LeadersService;
import org.example.studentsystem_back.mapper.LeadersMapper;
import org.example.studentsystem_back.util.JwtUtil;
import org.example.studentsystem_back.vo.leaderMangeVo.LeaderVo;
import org.example.studentsystem_back.vo.userManageVo.PageLeaderVo;
import org.example.studentsystem_back.vo.userManageVo.smallVo.PageLeaderItem;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 马鑫
* @description 针对表【leaders】的数据库操作Service实现
* @createDate 2024-07-10 13:50:19
*/
@Service
public class LeadersServiceImpl extends ServiceImpl<LeadersMapper, Leaders>
    implements LeadersService{
    @Resource
    private LeadersMapper leadersMapper;
    @Resource
    private UsersMapper usersMapper;

    @Override
    public CommonResponse<PageLeaderVo> getLeaders(int limit, int page){
        int total= Math.toIntExact(leadersMapper.selectCount(null));
        limit=page*(limit-1);
        List<Leaders> list = leadersMapper.getLeadersByPage(page,limit);
        List<PageLeaderItem> pageLeaderItems = new ArrayList<>();
        for (Leaders leaders : list) {
            PageLeaderItem pageLeaderItem = new PageLeaderItem();
            pageLeaderItem.setLeaders(leaders);
            Users users=usersMapper.selectById(leaders);
            pageLeaderItem.setName(users.getName());
            pageLeaderItems.add(pageLeaderItem);
        }
        PageLeaderVo vo = new PageLeaderVo();
        vo.setTotal(total);
        vo.setPageList(pageLeaderItems);
        return CommonResponse.createForSuccess("查询成功",vo);
    }

    @Override
    public CommonResponse<LeaderVo> getLeaderOne(String userid)
    {
        Users users=usersMapper.selectById(userid);
        QueryWrapper<Leaders> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userid",userid);
        Leaders leaders=leadersMapper.selectOne(queryWrapper);
        LeaderVo vo = new LeaderVo();
        vo.setUser(users);
        vo.setLeader(leaders);
        return CommonResponse.createForSuccess("查询成功",vo);
    }

    @Override
    public CommonResponse<LeaderVo> insertLeaders(LeaderVo leaderVo){
        Leaders leaders=new Leaders();
//        leaders=leaderVo.getLeader();
        Users users=new Users();
//        users=leaderVo.getUser();
        usersMapper.insert(users);
        leadersMapper.insert(leaders);
        return CommonResponse.createForSuccess("添加成功",leaderVo);
    }

    @Override
    public CommonResponse<Leaders> updateLeaders(Leaders leaders){
        leadersMapper.updateById(leaders);
        return CommonResponse.createForSuccess("更新成功",leaders);
    }

    @Override
    public CommonResponse<Leaders> deleteLeaders(String id){
        usersMapper.deleteById(id);
        QueryWrapper<Leaders> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userid",id);
        leadersMapper.delete(queryWrapper);
        return CommonResponse.createForSuccess("删除成功",leadersMapper.selectById(id));
    }

    @Override
    public CommonResponse<LeaderVo> getCurrentLeader(HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("token");
        JwtUtil.verifyToken(token);
        Users user = JwtUtil.getCurrentUser(token);
        QueryWrapper<Leaders> queryWrapper = new QueryWrapper<>();
        if (user != null) {
            queryWrapper.eq("userid",user.getUserid());
        }
        Leaders leaders = leadersMapper.selectOne(queryWrapper);
        if(leaders==null)
        {
            return CommonResponse.createForSuccess("没有该领导信息",null);
        }
        LeaderVo leaderVo = new LeaderVo();
        leaderVo.setLeader(leaders);
        if (user != null) {
            leaderVo.setUser(user);
        }
        return CommonResponse.createForSuccess("查询成功",leaderVo);
    }
}




